package chapter1.classTest.task;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
附件《lottery2.txt》是最近几年中国福利彩票的中奖号码，
存储格式是 期号:红球号码用空格隔开，后面是+篮球号，统计出现次数最多的红球号码和蓝球号码
 */
public class Task6 {
    public static void main(String[] args) throws Exception {
        HashMap<String, Integer> map = new HashMap<>();
        HashMap<String, Integer> map2 = new HashMap<>();
        BufferedReader in = new BufferedReader(new FileReader("F:\\123\\lottery2.txt"));
        String s = "";
        String red = "", blue = ""; //红，蓝球号码
        int a = 0, b = 0;
        int countRed = 1, countBlue = 1; //假设红球和篮球都只有一个重复
        while ((s = in.readLine()) != null) {
            a = s.indexOf(':');
            b = s.indexOf('+');
            red = s.substring(a + 1, b);

            String[] str = red.split("\\s");  //空格
            for (int i = 0; i < str.length; i++) {
                if (map.containsKey(str[i]))//红球
                    map.put(str[i], map.get(str[i]) + 1);
                else
                    map.put(str[i], 1);
            }


            blue = s.substring(b + 1);
            if (map2.containsKey(blue)) //篮球
                map2.put(blue, map2.get(blue) + 1);
            else
                map2.put(blue, 1);
        }

        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> c : entries) {
            if (c.getValue() > countRed) {
                countRed = c.getValue();
                red = c.getKey();
            }
        }
        if (countRed > 1)
            System.out.println("红球号码重复最多的为：" + red + "，重复次数：" + countRed);


        Set<Map.Entry<String, Integer>> entries2 = map2.entrySet();
        for (Map.Entry<String, Integer> c : entries2) {
            if (c.getValue() > countBlue) {
                countBlue = c.getValue();
                blue = c.getKey();
            }
        }
        if (countBlue > 1)
            System.out.println("篮球号码重复最多的为：" + blue + "，重复次数：" + countBlue);


    }
}
